﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GRC_TPE_WebApp.Model;

namespace GRC_TPE_WebApp.Controller
{
    public static class ClientController
    {
        /// <summary>
        /// Permet d'ajouter un client en base
        /// </summary>
        /// <param name="name"></param>
        /// <param name="activity"></param>
        /// <param name="adresse"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string AddClient(String name, String activity, String adresse, int phone)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                //Création d'un client temporaire, afin d'effectuer l'insertion en base
                Client currentClient = new Client();

                //Ajout des paramètres au client courant
                currentClient.Name = name;
                currentClient.Activity = activity;
                currentClient.Adresse = adresse;
                currentClient.Phone = "0"+phone.ToString();

                //Ajout en base
                database.AddToClientSet(currentClient);

                //Commit
                database.SaveChanges();

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return "Erreur lors de l'ajout d'un client";
            }
        }

        /// <summary>
        /// Méthode récupérant un client en base avec son id
        /// </summary>
        /// <param name="idClient"></param>
        /// <returns></returns>
        public static string[] GetClientFields(int idClient)
        {
            try
            {
                string[] ret = null;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération du client par son id
                Client existingClient = (from cli in database.ClientSet
                                       where cli.Id == idClient
                                       select cli).First();

                if (existingClient != null)
                {
                    ret = new string[4];

                    ret[0] = existingClient.Name;
                    ret[1] = existingClient.Activity;
                    ret[2] = existingClient.Adresse;
                    ret[3] = existingClient.Phone.ToString();
                }
                else
                {
                    ret = new string[1];
                    ret[0] = String.Format("Le client d'id '{0}' n'existe pas.", idClient);
                }

                return ret;
            }
            catch (Exception ex)
            {
                string[] ret = new string[1];

                ret[0] = String.Format("Une erreur est survenue lors de la récupération du client d'id '{0}' en base.", idClient);

                return ret;
            }
        }

        /// <summary>
        /// Méthode de modification d'un client en base
        /// </summary>
        /// <param name="idClient"></param>
        /// <param name="name"></param>
        /// <param name="activity"></param>
        /// <param name="address"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string EditClient(int idClient, string name, string activity, string address, int phone)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération du client par son id
                Client existingClient = (from cli in database.ClientSet
                                             where cli.Id == idClient
                                             select cli).First();

                if (existingClient != null)
                {
                    existingClient.Name = name;
                    existingClient.Activity = activity;
                    existingClient.Adresse = address;
                    existingClient.Phone = "0" + phone.ToString();
                }
                else
                {
                    ret = String.Format("Le client d'id '{0}' n'existe pas.", idClient);
                }

                database.SaveChanges();

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return string.Format("Erreur lors de la mise à jour du client d'id '{0}'.", idClient);
            }
        }

        /// <summary>
        /// Suppression d'un client
        /// </summary>
        /// <param name="idClient"></param>
        /// <returns></returns>
        public static string DeleteClient(int idClient)
        {
            try
            {
                string ret = string.Empty;

                Model_GRC_TPEContainer database = new Model_GRC_TPEContainer();

                /// récupération du client par son id
                Client existingClient = (from cli in database.ClientSet
                                         where cli.Id == idClient
                                         select cli).First();

                if (existingClient != null)
                {
                    database.DeleteObject(existingClient);
                    database.SaveChanges();
                }
                else
                {
                    ret = String.Format("Le client d'id '{0}' n'existe pas.", idClient);
                }

                ret = "No error";

                return ret;
            }
            catch (Exception ex)
            {
                return string.Format("Erreur lors de la suppression du client d'id '{0}'.", idClient);
            }
        }
    }
}